﻿#include "importcfgdialog.h"
#include "ui_importcfgdialog.h"

importCfgDialog::importCfgDialog(QWidget *parent, const QString& version) :
    QDialog(parent),
    ui(new Ui::importCfgDialog),
    scadeVersion(version)// 正确存储版本号 将version传给了scadeversion
{
    ui->setupUi(this);

//    qDebug()<<version;
    initialize();
}

importCfgDialog::~importCfgDialog()
{
    delete ui;
}

// 初始化函数：设置默认值和加载配置
void importCfgDialog::initialize(){

    ui->etp_lineEdit->setText(u8"请选择SCADE A661工程的etp文件");
    ui->sdy_lineEdit->setText(u8"请选择SCADE 变量关联的sdy文件");
}


void importCfgDialog::on_etp_pushButton_clicked()
{
    // 检查 etp_lineEdit 上的文本是否修改
    QString currentText = ui->etp_lineEdit->text();

    // 判断文本是否为空或为默认提示
    if(currentText == "请选择SCADE A661工程的etp文件" || currentText.isEmpty()){
        // 弹出文件对话框选择文件夹路径
        QString path = QFileDialog::getOpenFileName(this, u8"请选择SCADE A661工程的etp文件", "C:/Users", u8"可执行文件 (*.etp)");

        if (!path.isEmpty()) {
            // 更新配置文件路径
            importCfg.etpFilePath = path;

            // 更新界面上显示的路径
            ui->etp_lineEdit->setText(path);
        }
    }
    else{
        // 如果按钮上已有文本，弹出文件对话框，默认路径为按钮当前显示的路径
        QString path=QFileDialog::getOpenFileName(this, u8"请选择SCADE A661工程的etp文件", currentText, u8"可执行文件 (*.etp)");

        // 更新配置文件路径
        importCfg.etpFilePath = path;

        ui->etp_lineEdit->setText(path);
    }
}

void importCfgDialog::on_sdy_pushButton_clicked()
{
    // 检查 sdy_lineEdit 上的文本是否修改
    QString currentText = ui->sdy_lineEdit->text();

    // 判断文本是否为空或为默认提示
    if(currentText == "请选择SCADE 变量关联的sdy文件" || currentText.isEmpty()){
//        // 弹出文件对话框选择文件夹路径
//        QString path = QFileDialog::getExistingDirectory(this, "请选择SCADE 变量关联的sdy文件", "C:/Users");
        // 弹出文件对话框选择文件夹路径   getOpenFileName选取sdy文件路径
        QStringList paths = QFileDialog::getOpenFileNames(this, u8"请选择SCADE 变量关联的sdy文件", "C:/Users", u8"可执行文件 (*.sdy)");

        if (!paths.isEmpty()) {
            // 更新配置文件路径
            importCfg.sdyFilePath = paths;

            // 更新界面上显示的路径
            ui->sdy_lineEdit->setText(paths.join("; "));
        }
    }

    else{
//        // 如果按钮上已有文本，弹出文件对话框，默认路径为按钮当前显示的路径
//        QString path=QFileDialog::getExistingDirectory(this,"请选择SCADE 变量关联的sdy文件",currentText);
        // 弹出文件对话框选择文件夹路径   getOpenFileName选取sdy文件路径
        QStringList paths = QFileDialog::getOpenFileNames(this, u8"请选择SCADE 变量关联的sdy文件", currentText, u8"可执行文件 (*.sdy)");

        // 更新配置文件路径
        importCfg.sdyFilePath = paths;

        ui->sdy_lineEdit->setText(paths.join("; "));
    }
}

void importCfgDialog::on_ok_pushButton_clicked()
{
    //根据输入文件进行工程转换
    //1、etp模型文件转换
    QString etpPath = importCfg.etpFilePath;
    QString etpPathAbs = QFileInfo(etpPath).absolutePath();

    //2、sdy关联文件转换
    QStringList sdyPathList = importCfg.sdyFilePath;

    //etp文件获取sgfx文件路径
    EtpXmlData etpXmlData;
    QList<SGFX> sgfxList;
    Rules rules;
    ExtensionRules eRules;
    PropTypes propTypes;
    CommuParams commuParams;
    QMap<QString, QString> constMap;

    writera661 a661;
    QString projectName;
    QString genEtpPath;
    QStringList genRdmfPath;
    QString genResourcePath;

    SdyContent sdys;

    findSgfx findsgfx;
    findsgfx.parseEtpFile(etpPath, etpXmlData);

    findsgfx.parseSdyFiles(sdyPathList, sdys);

    QDir appDir(QCoreApplication::applicationDirPath());
    appDir.cdUp(); // 向上移动一层
    QString basePath = appDir.absolutePath(); // 获取绝对路径

    // 拼接目标路径
    QString rulesPath = basePath + "/A661 description/a661_description24";
    findsgfx.rulesRead(rules, rulesPath, propTypes, constMap, commuParams);

    QString eRulesPath = rulesPath + "/WidgetExtensions";
    findsgfx.extensionRulesRead(eRules, eRulesPath);

    projectName = QFileInfo(etpPath).baseName();
    genEtpPath = etpPathAbs + "/" + projectName + "_RD_AIR/" + projectName + ".etp";

    QStringList sgfxPath;
    //拼接sgfx文件的全路径
    for(int i = 0; i < etpXmlData.sgfxPath.size(); i++){
        sgfxPath.append(etpPathAbs + "/" + etpXmlData.sgfxPath.at(i));
        SGFX sgfx;
        sgfx.etpPath = QFileInfo(etpPath).fileName();
        sgfxList.append(sgfx);
        //解析sgfx文件
        findsgfx.parseSgfxFile(sgfxPath[i], sgfxList[i], constMap);
        genRdmfPath.append(etpPathAbs + "/" + projectName + "_RD_AIR/" + QFileInfo(sgfxPath.at(i)).fileName().chopped(5) + ".rdmf");
        etpXmlData.rdmfPath.append(genRdmfPath[i]);
    }

    genResourcePath = etpPathAbs + "/" + projectName + "_RD_AIR/";

    //写出etp文件
    a661.genEtpFile(genEtpPath, etpXmlData);

    for(int i = 0; i < genRdmfPath.size(); i++){
        //写出rdmf文件
        a661.genRdmfFile(genRdmfPath.at(i), sgfxList[i], rules, eRules, propTypes, constMap, commuParams, sdys);
    }

    //抓取资源文件给REDACE A661
    a661.genResourceFile(genResourcePath, etpPathAbs);
    a661.copyProjectFolder(etpPathAbs, genResourcePath);

    //弹出工程转换成功的message
    QMessageBox::information(this,tr(u8"工程转换成功"), tr(u8"工程文件已成功转换为REDACE A661格式！\n""生成路径: %1").arg(genResourcePath), QMessageBox::Ok);

    //输出工程转换的日志文件


    //退出此窗口
    this->close();

}

void importCfgDialog::on_cancel_pushButton_clicked()
{
    this->close();
}
